import * as gg from './global'
import uniRequest from 'uni-request'
import { getStore } from '../store/storePlugin'

// 请求拦截
uniRequest.interceptors.request.use(
    request => {
        //配置基本信息  
        // var lctoken = 'F6601074-B1A4-13E3-3577-390B02F1B964'  
        // if (lctoken) { 
        //     request.headers.token = lctoken
        // }
        uni.showLoading({
            title: '加载中',
            mask: true
        })
        return request;
    },
    err => {
        console.log('请求失败');
        return Promise.reject(err);
    });

// 响应拦截
uniRequest.interceptors.response.use(function (response) {
    if (response.data && response.data.error == 0) {
        setTimeout(function () {
            uni.hideLoading()
        }, 1500)
    } else {
        uni.hideLoading()
    }
    return Promise.resolve(response);
}, function (error) {
    console.log('返回进入拦截失败')
    return Promise.reject(error);
});
export const jsonpResource = function (url, data) {
    /* #ifdef H5 */
    return uniRequest.post('/api_ajax/' + url, data)
    /* #endif */

    /* #ifdef MP-WEIXIN */
    return uniRequest.post('https://gg.global-good.cn/api_ajax/' + url, data)
    /* #endif */
}
export const jsonpResource2 = function (data) {
    //let foshan_token = gg.getKey('token'), phone = gg.getKey('username'), url = gg.SMART_PIC_SRC;
    // /* #ifdef H5 */
    //return uniRequest.get('https://gg.global-good.cn/api_ajax/transfer.php?data=' + url + data + '/foshan_token/' + foshan_token + '/phone/' + phone)
     /* #endif */
 
     /* #ifdef MP-WEIXIN */
     return uniRequest.get(url + data + '/foshan_token/' + foshan_token + '/phone/' + phone)
     /* #endif */
}
export const jsonpResource3 = function (url="", data) {
    var token = (gg.getKey('token').length>1) ? gg.getKey('token') : getStore().getters.getToken
    if(token) {data.token=token} 
     /* #ifdef H5 */
     return uniRequest.post('/api/' + url, data)
     /* #endif */
 
     /* #ifdef MP-WEIXIN */
     return uniRequest.post(gg.APP_PIC_SRC + url, data)
     /* #endif */
}
// export const jsonpResource3 = function (url="", data, success, error) {
//     // APP_PIC_SRC
//     var device_time = '', device_version = '1.0', device_type = 'wechat'
//     /* #ifdef H5 */
//     // return uniRequest.post('/api_ajax/', data)
//     /* #endif */
//     /* #ifdef MP-WEIXIN */

//     // return uniRequest.post('https://gg.global-good.cn/api_ajax/' + url, data)

//     /* #endif */
    
//     data.device_time = parseInt(new Date().getTime() / 1000)
//     data.device_version = '1.0'
//     data.device_type = 'wechat'
//     uniRequest.post(gg.APP_PIC_SRC + 'api/Receive/getPostData', data).then((res) => {
//         if (res.data.Code = '200000') {
//             data.signature = res.data.returnData.signature
//             runajax(url, data, success, error)
//         } else {
//             success(res)
//         }
//     }, (err) => { console.log("Error signature") })


// }
// function runajax(url, data, success, error) {
//     uniRequest.post(gg.APP_PIC_SRC + url, data).then((res) => {
//         if (res.data.Code = '200000') {
//             console.log("sueess")
//             success(res)
//         }
//     }, (err) => { console.log("Unkwon Error"); error(err) })
// }